﻿using System;
using System.Data;
using MODEL;
using System.Net;
using System.IO;

public partial class FindPassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }

    protected void BindData()
    {
        DataTable dtUserInfo = BLL.User.GetReferee(Request.QueryString["userId"]);
        if (dtUserInfo.Rows.Count > 0)
        {
            LblQuestion1.Text = dtUserInfo.Rows[0]["Question1"].ToString();
            LblQuestion2.Text = dtUserInfo.Rows[0]["Question2"].ToString();
            LblQuestion3.Text = dtUserInfo.Rows[0]["Question3"].ToString();
            tb_user.Text = Request.QueryString["userId"].ToString();
        }
        else
        {
            CommenMethod.GetJavaScript("会员信息不存在！", "login.aspx");
        }
    }

    /// <summary>
    /// 提交密保问题
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string userId = Request.QueryString["userId"];
        string aa = Rb_FindPassword.SelectedValue;
        if (Rb_FindPassword.SelectedValue == "0")
        {
            if (string.IsNullOrEmpty(TxtAnswer1.Text.Trim()))
            {
                CommenMethod.GetJavaScript("请输入密保问题一答案！");
                return;
            }
            if (string.IsNullOrEmpty(TxtAnswer2.Text.Trim()))
            {
                CommenMethod.GetJavaScript("请输入密保问题二答案！");
                return;
            }
            if (string.IsNullOrEmpty(TxtAnswer3.Text.Trim()))
            {
                CommenMethod.GetJavaScript("请输入密保问题三答案！");
                return;
            }
            MODEL.User user = new User
            {
                Answer1 = CommenMethod.md5(TxtAnswer1.Text.Trim(), 32),
                Answer2 = CommenMethod.md5(TxtAnswer2.Text.Trim(), 32),
                Answer3 = CommenMethod.md5(TxtAnswer3.Text.Trim(), 32),
                UserID = userId
            };
            if (BLL.User.CheckQuestion(user))
            {
                if (string.IsNullOrEmpty(TxtLogin.Text.Trim()))
                {
                    CommenMethod.GetJavaScript("请输入登录密码！");
                    return;
                }
                if (string.IsNullOrEmpty(TxtLogin1.Text.Trim()))
                {
                    CommenMethod.GetJavaScript("请输入确认登录密码！");
                    return;
                }
                if (TxtLogin.Text.Trim() != TxtLogin1.Text.Trim())
                {
                    CommenMethod.GetJavaScript("新登录密码和确认密码不一致！");
                    return;
                }
                if (string.IsNullOrEmpty(TxtPay.Text.Trim()))
                {
                    CommenMethod.GetJavaScript("请输入二级密码！");
                    return;
                }
                if (string.IsNullOrEmpty(TxtPay1.Text.Trim()))
                {
                    CommenMethod.GetJavaScript("请输入确认二级密码！");
                    return;
                }
                if (TxtPay.Text.Trim() != TxtPay1.Text.Trim())
                {
                    CommenMethod.GetJavaScript("新二级密码和确认二级密码不一致！");
                    return;
                }
                BLL.User.UpdatePassword(userId, CommenMethod.md5(TxtLogin.Text.Trim(), 32), CommenMethod.md5(TxtPay.Text.Trim(), 32));
                CommenMethod.GetJavaScript("密码重置成功！", "login.aspx");
            }
            else
            {
                CommenMethod.GetJavaScript("密保问题不正确！");
            }
        }
        else
        {
            if (string.IsNullOrEmpty(tb_mobile.Text.Trim()))
            {
                CommenMethod.GetJavaScript("请输入注册时所留手机号！");
                return;
            }
            if (string.IsNullOrEmpty(tb_code.Text.Trim()))
            {
                CommenMethod.GetJavaScript("请输入验证码！");
                return;
            }
            if (!CommenMethod.IsMobile(tb_mobile.Text.Trim()))
            {
                CommenMethod.GetJavaScript("输入会员手机号码格式不正确");
                return;
            }
            //判断手机号是否存在
            bool tbuser = BLL.User.IsExistsMobile(tb_mobile.Text.Trim());
            if (!tbuser)
            {
                CommenMethod.GetJavaScript("该手机号不存在，请重新填写!");
                return;
            }
            DataTable mobiluser = BLL.User.GetUserInfo(" where userid='" + userId + "' and mobile='" + tb_mobile.Text.Trim() + "'");
            if (mobiluser.Rows.Count != 1)
            {
                CommenMethod.GetJavaScript("该会员账号绑定的手机号与填写手机号码不一致!");
                return;
            }

            //if (Request.Cookies["CheckCode"].Value != tb_code.Text.Trim().ToUpper())
            //{
            //    CommenMethod.GetJavaScript("验证码不正确");
            //    return;
            //}
            if (string.IsNullOrEmpty(tb_code.Text.Trim()))
            {
                CommenMethod.GetJavaScript("短信验证不能为空！");
                return;
            }
            else
            {

                string code = CommenMethod.GetSession("code").ToString();
                string codetime = CommenMethod.GetSession("codetime").ToString();
                if (codetime != "" && codetime != null)
                {
                    string timeold = DateTime.Parse(codetime).ToString("HH:mm:ss");
                    double dm = (DateTime.Now - Convert.ToDateTime(timeold)).TotalMinutes;

                    if (dm >= 10)
                    {
                        CommenMethod.GetJavaScript("短信验证码已过期,请重新操作");
                        return;
                    }
                }
                if (code != "" && code == tb_code.Text.Trim())
                {
                    if (string.IsNullOrEmpty(tb_Login.Text.Trim()))
                    {
                        CommenMethod.GetJavaScript("请输入登录密码！");
                        return;
                    }
                    if (string.IsNullOrEmpty(tb_Login1.Text.Trim()))
                    {
                        CommenMethod.GetJavaScript("请输入确认登录密码！");
                        return;
                    }
                    if (tb_Login.Text.Trim() != tb_Login1.Text.Trim())
                    {
                        CommenMethod.GetJavaScript("新登录密码和确认密码不一致！");
                        return;
                    }
                    if (string.IsNullOrEmpty(tb_Pay.Text.Trim()))
                    {
                        CommenMethod.GetJavaScript("请输入二级密码！");
                        return;
                    }
                    if (string.IsNullOrEmpty(tb_Pay1.Text.Trim()))
                    {
                        CommenMethod.GetJavaScript("请输入确认二级密码！");
                        return;
                    }
                    if (tb_Pay.Text.Trim() != tb_Pay1.Text.Trim())
                    {
                        CommenMethod.GetJavaScript("新二级密码和确认二级密码不一致！");
                        return;
                    }
                    BLL.User.UpdatePassword(userId, CommenMethod.md5(tb_Login.Text.Trim(), 32), CommenMethod.md5(tb_Pay.Text.Trim(), 32));
                    CommenMethod.GetJavaScript("密码重置成功！", "login.aspx");
                }
                else
                {
                    CommenMethod.GetJavaScript("输入验证码不正确，请重新输入！");
                    return;
                }
            }

        }
    }
  
}